package com.zxg.zbc.biz.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eleme.openapi.sdk.api.entity.order.OOrder;
import eleme.openapi.sdk.api.entity.order.OrderList;
import eleme.openapi.sdk.api.entity.product.OBackCategory;
import eleme.openapi.sdk.api.entity.product.OCategory;
import eleme.openapi.sdk.api.entity.product.OItem;
import eleme.openapi.sdk.api.entity.product.OItemIdWithSpecIds;
import eleme.openapi.sdk.api.entity.user.OUser;
import eleme.openapi.sdk.api.exception.ServiceException;
import eleme.openapi.sdk.api.service.OrderService;
import eleme.openapi.sdk.api.service.ProductService;
import eleme.openapi.sdk.api.service.UgcService;
import eleme.openapi.sdk.api.service.UserService;
import eleme.openapi.sdk.config.Config;
import eleme.openapi.sdk.oauth.OAuthClient;
import eleme.openapi.sdk.oauth.response.Token;

public class ElemeUtil {

	private final static Logger log = LoggerFactory.getLogger(ElemeUtil.class);

	// 变量为true: 沙箱环境 false: 生产环境
	static boolean  isSandbox = true;
    
    // 当前环境key
	static String appKey = "r2hm5Ftyns";
	static String callBack = "http://127.0.0.1:8080/";
    // 当前环境secret
	static String appSecret = "5e842d8f87eb093a014d1fd923bb978748b3780e";
	
	// 实例化一个配置类
	public static Config config = new Config(isSandbox, appKey, appSecret);
    
    // 使用config对象，实例化一个授权类
	public static OAuthClient client = new OAuthClient(config);
	
	
	public static String getAuthUrl(String state,String scope){
		return  client.getAuthUrl(callBack, scope==null?"all":scope, state);
	}
	public static Token getTokenByCode(String code){
		Token token = client.getTokenByCode(code, callBack);
		return token;
	}
	public static void main(String[] args) throws ServiceException {
		Config config2 = new Config(true, "waPqsb1kQn", "87a1d0cc1c05768d658f242c459664099e4a7a8d");
		OAuthClient client2 = new OAuthClient(config2);
		System.out.println(client2.getAuthUrl(callBack, "all","all"));
		String code2 = "ac76a5d4659644087de60746b7260c48";//token 69d63cf9af414b696d1e0d808aa50450
		Token token2 = client2.getTokenByCode(code2, callBack);
		System.out.println(token2.getAccessToken());
		//System.out.println(getAuthUrl("auth", null));*/
		
		String atoken = "c7662a44327ef0b361a7385f0a683f0e";
		String rtoken = "6ef760d9a5d0e9290a30d73ffbb6ed9e";
		Token token = new Token();
		token.setAccessToken(atoken);
		token.setRefreshToken(rtoken);
		
		
		UserService userService = new UserService(ElemeUtil.config, token);
		 OUser ouser = userService.getUser();
	        
		   long shop = ouser.getAuthorizedShops().get(0).getId();
		System.out.println(shop);
		System.out.println(ouser.getUserName());
		OrderService orderService = new OrderService(config, token);
		OrderList list = orderService.getAllOrders(shop, 1, 20, "2017-09-01");
		System.out.println(list.getTotal()+" ");
		OOrder order = list.getList().get(0);
		System.out.println(order.getAddress()+order.getId());
		
		order = orderService.getOrder(order.getId());
		System.out.println(order);
		
		
		ProductService productService = new ProductService(config, token);
		List<OBackCategory> cats = productService.getBackCategory(shop);
		System.out.println(cats);
		//productService.createCategory(shop, "蛋炒饭3", "一道美味的炒饭");
		//productService.createCategoryWithChildren(shop, "蛋炒饭2xxx", 528010503l, "一道美味的炒饭");
		List<OCategory> ocats = productService.getShopCategoriesWithChildren(shop);
		System.out.println(ocats);
		
		
		Map<Long, OItem> map = productService.getItemsByCategoryId(1230306942l);
		for(Long key:map.keySet()){
			System.out.println(map.get(key));
		}
		
		
		OItem i = productService.getItem(1263396268);
		System.out.println(i);
		
		List<OItemIdWithSpecIds> specIds = new ArrayList<OItemIdWithSpecIds>();
		OItemIdWithSpecIds oItemIdWithSpecIds = new OItemIdWithSpecIds();
		oItemIdWithSpecIds.setItemId(i.getId());
		List<Long> itemSpecIds = new ArrayList<Long>();

		itemSpecIds.add(i.getSpecs().get(0).getSpecId());
		oItemIdWithSpecIds.setItemSpecIds(itemSpecIds);
		specIds.add(oItemIdWithSpecIds);
		productService.batchOffShelf(specIds);
		UgcService ugcService = new UgcService(config, token);
		//ugcService.getOrderRateByOrderId("1201959285783069724");
		
	}
}
